package ui;

/**
 * JPanel HandleAccounts
 * 
 * @author XA9
 * 
 */

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

import db.Account;
import db.BankDatabase;

public class HandleAccounts extends JPanel {
	private JLabel lblEmployeeMenu;
	private JLabel lblXA9;
	private JLabel lblLogo;
	private JButton btnDelete;
	private JButton btnAddInterest;
	private JButton btnReturn;
	private JTable tblAccounts;
	private JScrollPane scpAccounts;
	private JButton btnAddInterestAll;
	private JButton btnAddAccount;
	private DefaultTableModel tableModel;
	private JLabel lblPleaseSelectAn;
	/** bankDatabase */
	private BankDatabase bankDatabase;
	/** table accountId */
	private String accountId = "";
	/** table rateId */
	private String rateId = "";

	/**
	 * Create the panel.
	 */
	public HandleAccounts() {

		this.bankDatabase = new BankDatabase();

		setBackground(UIManager.getColor("Label.background"));
		setLayout(null);

		lblEmployeeMenu = new JLabel("Account administration");
		lblEmployeeMenu.setOpaque(true);
		lblEmployeeMenu.setBackground(UIManager.getColor("Label.background"));
		lblEmployeeMenu.setHorizontalAlignment(SwingConstants.CENTER);
		lblEmployeeMenu.setForeground(Color.BLACK);
		lblEmployeeMenu.setFont(new Font("Eurostile", Font.BOLD, 22));
		lblEmployeeMenu.setBounds(0, 94, 735, 28);
		add(lblEmployeeMenu);

		lblXA9 = new JLabel("\u00A92013 CBS HA.IT (Gruppe XA-9)\t\t ");
		lblXA9.setForeground(UIManager.getColor("Button.darkShadow"));
		lblXA9.setBackground(UIManager.getColor("Button.darkShadow"));
		lblXA9.setHorizontalAlignment(SwingConstants.CENTER);
		lblXA9.setFont(new Font("Eurostile", Font.PLAIN, 11));
		lblXA9.setBounds(0, 550, 735, 50);
		add(lblXA9);

		btnDelete = new JButton("Delete");
		btnDelete.setFont(new Font("Dialog", Font.BOLD, 11));
		btnDelete.setBounds(74, 324, 185, 50);
		add(btnDelete);

		btnAddInterest = new JButton("Add interest");
		btnAddInterest.setFont(new Font("Dialog", Font.BOLD, 11));
		btnAddInterest.setBounds(266, 324, 200, 50);
		add(btnAddInterest);

		btnReturn = new JButton("Return to main menu");
		btnReturn.setForeground(UIManager.getColor("CheckBox.select"));
		btnReturn.setFont(new Font("Eurostile", Font.PLAIN | Font.BOLD, 13));
		btnReturn.setBounds(74, 380, 185, 45);
		add(btnReturn);

		lblLogo = new JLabel("");
		lblLogo.setIcon(new ImageIcon(
				"/Users/martinotto-sidelmann/Desktop/Zombium.png"));
		lblLogo.setBounds(530, 12, 200, 70);
		add(lblLogo);

		scpAccounts = new JScrollPane();
		scpAccounts.setBounds(77, 152, 582, 160);
		scpAccounts.setFont(new Font("Eurostile", Font.PLAIN, 13));
		add(scpAccounts);

		this.tableModel = new DefaultTableModel()
		  {
		    public boolean isCellEditable(int row, int column)
		    {
		      return false;
		    }
		  };

		tblAccounts = new JTable(this.tableModel);
		tblAccounts.setRowSelectionAllowed(true);
		ListSelectionModel cellSelectionModel = tblAccounts.getSelectionModel();
		cellSelectionModel
				.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		cellSelectionModel
				.addListSelectionListener(new ListSelectionListener() {
					public void valueChanged(ListSelectionEvent e) {
						if (e.getValueIsAdjusting()) {
							return;
						}

						int[] selectedRowAccountNumber = tblAccounts
								.getSelectedRows();
						int[] selectedColumnsAccountNumber = tblAccounts
								.getSelectedColumns();
						int[] selectedRowRateId = tblAccounts.getSelectedRows();
						int[] selectedColumnsRateId = tblAccounts
								.getSelectedColumns();

						if (selectedRowAccountNumber.length <= 0) {
							return;
						}

						String selectedData = tblAccounts.getValueAt(
								selectedRowAccountNumber[0], 0).toString();
						String selectedDataRate = tblAccounts.getValueAt(
								selectedRowRateId[0], 3).toString();
						rateId = selectedDataRate;
						accountId = selectedData;
						System.out.println("Selected AccountNumber: "
								+ selectedData);
						System.out.println("Selected RateId: "
								+ selectedDataRate);
					}

				});

		this.tableModel.addColumn("Accountnumber");
		this.tableModel.addColumn("Account");
		this.tableModel.addColumn("Balance");
		this.tableModel.addColumn("Rate");
		this.tableModel.addColumn("LoginId");

		scpAccounts.setViewportView(tblAccounts);
		tblAccounts.setBackground(Color.WHITE);
		tblAccounts.setRowHeight(25);
		tblAccounts.setFont(new Font("Eurostile", Font.PLAIN, 11));
		tblAccounts.getTableHeader();

		btnAddInterestAll = new JButton("Add interest all accounts");
		btnAddInterestAll.setFont(new Font("Dialog", Font.BOLD, 11));
		btnAddInterestAll.setBounds(266, 377, 200, 50);
		add(btnAddInterestAll);

		btnAddAccount = new JButton("Add new account");
		btnAddAccount.setFont(new Font("Dialog", Font.BOLD, 11));
		btnAddAccount.setBounds(474, 324, 185, 50);
		add(btnAddAccount);
		tblAccounts.getTableHeader();
		tblAccounts.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		lblPleaseSelectAn = new JLabel(
				"Please select an account before pressing a button");
		lblPleaseSelectAn.setHorizontalAlignment(SwingConstants.CENTER);
		lblPleaseSelectAn.setForeground(Color.BLACK);
		lblPleaseSelectAn.setFont(new Font("Eurostile", Font.PLAIN, 14));
		lblPleaseSelectAn.setBounds(0, 124, 735, 16);
		add(lblPleaseSelectAn);

	}

	/**
	 * Display accounts from database in JTable
	 * 
	 */
	public void displayAccounts() {

		this.tableModel.setRowCount(0);

		ArrayList<Account> accounts = this.bankDatabase.getAccounts();

		for (Account account : accounts) {
			Object[] data = { account.getAccountnumber(), account.getAccount(),
					account.getAmount(), account.getRate(),
					account.getLoginId() };
			this.tableModel.addRow(data);
		}
	}

	/**
	 * Returns value of selected rows accountId
	 * 
	 * @return String accountId
	 */
	public String getAccountId() {
		return this.accountId;
	}

	
	/**
	 * Sets value of selected rows accountId 
	 * flush accountId value
	 * 
	 * @return String accountId
	 */
	public String setAccountId() {
		return accountId = "";
	}
	
	/**
	 * Returns value of selected rows rateId
	 * 
	 * @return String rateId
	 */
	public String getRateId() {
		return this.rateId;
	}

	/**
	 * Returns JButton
	 * 
	 * @return btnAddAccount
	 */
	public JButton getBtnAddAccount() {
		return btnAddAccount;
	}

	/**
	 * Returns JButton
	 * 
	 * @return btnDelete
	 */
	public JButton getBtnDelete() {
		return btnDelete;
	}

	/**
	 * Returns JButton
	 * 
	 * @return btnAddInterest
	 */
	public JButton getBtnAddInterest() {
		return btnAddInterest;
	}

	/**
	 * Returns JButton
	 * 
	 * @return btnAddInterestAll
	 */
	public JButton getBtnAddInterestAll() {
		return btnAddInterestAll;
	}

	/**
	 * Returns JButton
	 * 
	 * @return btnReturn
	 */
	public JButton getBtnReturn() {
		return btnReturn;
	}

	/**
	 * Add ActionListener to JButtons
	 * 
	 * @param ActionListener
	 *            l
	 * 
	 */
	public void addActionListener(ActionListener l) {
		btnDelete.addActionListener(l);
		btnAddAccount.addActionListener(l);
		btnAddInterestAll.addActionListener(l);
		btnAddInterest.addActionListener(l);
		btnReturn.addActionListener(l);
	}
}
